package com.playplus.utils.inapp
{
	import com.playplus.squeez.data.GameData;
	import com.playplus.squeez.utils.log.Log;
	import com.pozirk.payment.android.InAppPurchase;
	import com.pozirk.payment.android.InAppPurchaseDetails;
	import com.pozirk.payment.android.InAppPurchaseEvent;

	public class InAppPurchaseManager extends InApp
	{
		private var mInApp:InAppPurchase;
		public function InAppPurchaseManager()
		{
			super();
		}
		
		override public function init():void
		{
			if(!GameData.getInstance().isInAppAble) return;
			if(!mInApp){
				mInApp = new InAppPurchase();
			}
			
			initEvent();
			
			var mProductList:Array = new Array;
			
			mProductList[0] = "xxx.xxx.xxx.coin1000";
			
			mProductList[1] = "android.test.purchased";
			//Log.popup("init : ");
			mInApp.init("xxxxx");
			//mInApp.getProductsInfo(mProductList);
		}
		
		protected function initEvent():void
		{
			//Log.popup("initEvent : ");
			mInApp.addEventListener(InAppPurchaseEvent.INIT_SUCCESS, onInitSuccess);
			mInApp.addEventListener(InAppPurchaseEvent.INIT_ERROR, onInitError);
			
			mInApp.addEventListener(InAppPurchaseEvent.PURCHASE_SUCCESS, onPurchaseSuccess);
			mInApp.addEventListener(InAppPurchaseEvent.PURCHASE_ALREADY_OWNED, onPurchaseSuccess);
			mInApp.addEventListener(InAppPurchaseEvent.PURCHASE_ERROR, onPurchaseError);
			mInApp.addEventListener(InAppPurchaseEvent.RESTORE_SUCCESS, onRestoreSuccess);
			mInApp.addEventListener(InAppPurchaseEvent.RESTORE_ERROR, onRestoreError);
			mInApp.addEventListener(InAppPurchaseEvent.CONSUME_SUCCESS, onConsumeSuccess);
			mInApp.addEventListener(InAppPurchaseEvent.CONSUME_ERROR, onConsumeError);
			
			
		}
		
		protected function removeEvent():void
		{
						
			mInApp.removeEventListener(InAppPurchaseEvent.INIT_SUCCESS, onInitSuccess);
			mInApp.removeEventListener(InAppPurchaseEvent.INIT_ERROR, onInitError);
			
			mInApp.removeEventListener(InAppPurchaseEvent.PURCHASE_SUCCESS, onPurchaseSuccess);
			mInApp.removeEventListener(InAppPurchaseEvent.PURCHASE_ALREADY_OWNED, onPurchaseSuccess);
			mInApp.removeEventListener(InAppPurchaseEvent.PURCHASE_ERROR, onPurchaseError);
			mInApp.removeEventListener(InAppPurchaseEvent.RESTORE_SUCCESS, onRestoreSuccess);
			mInApp.removeEventListener(InAppPurchaseEvent.RESTORE_ERROR, onRestoreError);
			mInApp.removeEventListener(InAppPurchaseEvent.CONSUME_SUCCESS, onConsumeSuccess);
			mInApp.removeEventListener(InAppPurchaseEvent.CONSUME_ERROR, onConsumeError);
		}
		
		protected function onInitSuccess(event:InAppPurchaseEvent):void
		{
			//you can restore previously purchased items here
		}
		
		protected function onInitError(event:InAppPurchaseEvent):void
		{
			trace(event.data); //trace error message
			//Log.popup("onInitError : " + String(event.data));
		}
		protected function onPurchaseSuccess(event:InAppPurchaseEvent):void
		{
			trace(event.data); //product id
			//Log.popup("onPurchaseSuccess : " + String(event.data));
			restore();
		}
		
		protected function onPurchaseError(event:InAppPurchaseEvent):void
		{
			trace(event.data); //trace error message
			cancle();
		}
		protected function onRestoreSuccess(event:InAppPurchaseEvent):void
		{
			var purchase:InAppPurchaseDetails = mInApp.getDetails(mProductId);
			
			Log.print("purchase._type  : " + purchase._type);
			Log.print("purchase._orderId  : " + purchase._orderId);
			Log.print("purchase._packageName  : " + purchase._packageName);
			Log.print("purchase._sku  : " + purchase._sku);
			Log.print("purchase._time  : " + purchase._time);
			Log.print("purchase._purchaseState  : " + purchase._purchaseState);
			Log.print("purchase._payload  : " + purchase._payload);
			Log.print("purchase._token  : " + purchase._token);
			Log.print("purchase._json  : " + purchase._json);
			Log.print("purchase._signature  : " + purchase._signature);
			
			
			mInApp.consume(mProductId);
			
		}
		
		protected function onConsumeSuccess(event:InAppPurchaseEvent):void
		{
			trace(event.data); //trace error message
			Log.print("onConsumeSuccess  : " + JSON.stringify(event.data));
			complete();
		}
		
		protected function onConsumeError(event:InAppPurchaseEvent):void
		{
			trace(event.data); //trace error message
			cancle();
		}
		
		
		protected function onRestoreError(event:InAppPurchaseEvent):void
		{
			trace(event.data); //trace error message
			cancle();
		}
		
		override public function buyItem(pProductID:String):void
		{
			if(!GameData.getInstance().isInAppAble) return;
			//pProductID = "android.test.purchased";
			mInApp.purchase(pProductID, InAppPurchaseDetails.TYPE_INAPP);
			mProductId = pProductID;
		}
		
		public function restore():void
		{
			mInApp.restore();
		}
	}
}